use std::collections::HashMap;

use fudata::model::{kline_flat::KlineFlat, kline::Kline};
use serde::Serialize;
use ts_rs::TS;

impl From<Kline> for FinancialDataPoint {
    fn from(kline: Kline) -> Self {
        let klid: String = kline.id.clone().unwrap().id.to_raw();
        FinancialDataPoint {
            x: kline.klts.timestamp_millis(),
            y: kline.close,
            o: kline.open,
            h: kline.high,
            l: kline.low,
            c: kline.close,
            klid,
        }
    }
}

impl From<&Kline> for FinancialDataPoint {
    fn from(kline: &Kline) -> Self {
        let klid: String = kline.id.clone().unwrap().id.to_raw();
        FinancialDataPoint {
            x: kline.klts.timestamp_millis(),
            y: kline.close,
            o: kline.open,
            h: kline.high,
            l: kline.low,
            c: kline.close,
            klid,
        }
    }
}

impl From<KlineFlat> for FinancialDataPoint {
    fn from(kline: KlineFlat) -> Self {
        let klid: String = kline.id.clone().unwrap().id.to_raw();
        FinancialDataPoint {
            x: kline.klts.timestamp_millis(),
            y: kline.close,
            o: kline.open,
            h: kline.high,
            l: kline.low,
            c: kline.close,
            klid,
        }
    }
}

impl From<&KlineFlat> for FinancialDataPoint {
    fn from(kline: &KlineFlat) -> Self {
        let klid: String = kline.id.clone().unwrap().id.to_raw();
        FinancialDataPoint {
            x: kline.klts.timestamp_millis(),
            y: kline.close,
            o: kline.open,
            h: kline.high,
            l: kline.low,
            c: kline.close,
            klid,
        }
    }
}

/// 定义FinancialDataPoint结构体，与chartjs-chart-financial插件兼容
#[derive(Debug, Serialize, Default, TS)]
#[ts(export, export_to = "../../src/lib/bindings/FinancialDataPoint.ts")]
pub struct FinancialDataPoint {
    /// 时间戳
    x: i64,
    /// 值, 常用收盘价
    y: f64,
    /// 开盘价
    o: f64,
    /// 最高价
    h: f64,
    /// 最低价
    l: f64,
    /// 收盘价
    c: f64,
    /// KlineId
    klid: String,
}

/// 定义FinancialDataPoint结构体，与chartjs-chart-financial插件兼容
#[derive(Debug, Serialize, Default, TS)]
#[ts(export, export_to = "../../src/lib/bindings/ChartJsDataWrapper.ts")]
pub struct ChartJsDataWrapper {
    /// k线数据转换后时候直接展示在ChartJs线图上
    pub klines: Option<Vec<FinancialDataPoint>>,
    /// 指标数据转换后直接展示在ChartJs线图上
    pub indicators: Option<HashMap<String, Vec<FinancialDataPoint>>>,
    /// 均线数据转换后直接展示在ChartJs线图上
    pub malines: Option<HashMap<String, Vec<FinancialDataPoint>>>,
}
